define(function (require) {
    var _common = require('common/main');
    var _config = require('common/config');
    var _send = _common.sendRequest;
    function init() {
    	$('#new_place_confirm').click(function (){
    		alert(111);
    	});
    }
    function _product_render(data) {
    	var _html = '';
    	var _len = data.length;
		for (var i = 0; i < _len; i++) {
		    var _item = data[i];
		    _html += '<input class="radio" type="radio" name="optionsRadios" id="product'
		    	+_item.productId+'" value="'
		    	+_item.productName+'">';
		    _html +=_item.productName;
		    _html += '</input>';
		}
		$('#quotaProductDiv').html(_html);
		$('#quotaProductDiv #product1').prop("checked",true);
		_html = '<select class="input-medium search-query">';
		_html += '<option value="username">按username查询</option>';
    	_html += '<option value="userid">按userid查询</option>';
    	_html += '<option value="token">按token查询</option>';
    	_html += '</select>';
    	_html += '<input type="text">';
    	_html += '<button id="batchUserQuery" type="button" class="btn btn-primary" data-toggle="button">查询</button>';
    	$('#quotaQueryUser').html(_html);
    	_bindQuery();
    }
    function _query_render(data) {
    	var _html = '<table class="table">';
    	_html += '<thead><tr class="info">';
    	_html += '<th><input id="checkboxswith" type="checkbox"/></th>';
    	_html += '<th>userid</th>';
    	_html += '<th>username</th>';
    	_html += '<th>token</th>';
    	_html += '<th>token权限</th>';
    	_html += '<th>token类型</th>';
    	_html += '<th>上周使用配额</th>';
    	_html += '<th>本周分配配额</th>';
    	_html += '<th>本周使用配额</th>';
    	_html += '</tr></thead>';
    	_html += '<tbody id="databody">';
    	var _len = data.length;
		for (var i = 0; i < _len; i++) {
			var _item = data[i];
			_html += '<tr class="success">';
			_html += '<td>';
			_html += '<input id="'+_item.userid+'" type="checkbox"/>';
			_html += '</td>';
			_html += '<td>';
			_html += _item.userid;
			_html += '</td>';
			_html += '<td>';
			_html += _item.username;
			_html += '</td>';
			_html += '<td>';
			_html += _item.token;
			_html += '</td>';
			_html += '<td>';
			_html += _item.tokenDesc;
			_html += '</td>';
			_html += '<td>';
			_html += _item.tokentype;
			_html += '</td>';
			_html += '<td>';
			_html += _item.lastUquota;
			_html += '</td>';
			_html += '<td>';
			_html += _item.quota;
			_html += '</td>';
			_html += '<td>';
			_html += _item.uquota;
			_html += '</td>';
			_html += '</tr>';
		}
		_html += '</tbody></table>';
		_html += '<input type="text">';
		_html += '<button id="openAPIToken" type="button" class="btn btn-primary" data-toggle="button">添加配额</button>';
		$('#quotaShowQueryInfo').html(_html);
		_bindShow();
    }
    
    function _getAllProductLine()
    {
    	var _param = {};
    	_send("/role/product/list.ajax", _param, _product_render);
    }
    
    function _openAPIToken()
    {
    	var _param = [];
    	var _checkbox = $('#quotaShowQueryInfo #databody td input');
    	var _quota = $('#quotaShowQueryInfo input:text').val();
    	var _len = _checkbox.length;
    	for (var i = 0; i < _len; i++) 
    	{
            var _item = _checkbox[i];
            if (_item.checked)
            {
           	 	_param.push(_item.id);
       	 	}
   	 	}
    	var _productId = $('#quotaProductDiv input:radio:checked').attr("id").substring(7);
    	_send("/role/quota/addQuota.ajax",_common.stringify({userid:_param,productId: _productId,quota:_quota}),_confirmResult);
    }
    function _confirmResult(data)
    {
    	if(data)
    	{
    		_batchUserQuery();
    	}
    }
    
    function _batchUserQuery()
    {
    	var _productId = $('#quotaProductDiv input:radio:checked').attr("id");
    	var _queryWay = $('#quotaQueryUser select option:selected').attr("value");
    	var _queryInfo = $('#quotaQueryUser input').val();
    	_productId = _productId.substring(7);
    	var _param = _common.stringify({productId: _productId,queryWay: _queryWay,queryInfo: _queryInfo});
    	_send("/role/quota/query.ajax", _param, _query_render);
    }
    
    function _bindShow()
    {
    	$('#quotaShowQueryInfo #openAPIToken').click(_openAPIToken);
    	$('#quotaShowQueryInfo #checkboxswith').click(function (){
    		var c = $('#quotaShowQueryInfo #checkboxswith').prop("checked"); 
    		$('#quotaShowQueryInfo #databody td input').prop("checked",c);
    	});
    }
    
    function _bindQuery()
    {
    	$('#quotaQueryUser #batchUserQuery').click(_batchUserQuery);
    }
    
    function _bind() {
        $('#quotaTab').click(_getAllProductLine);
    }
    return { init: init };
});